package com.tledu.spring.controller;

import com.tledu.spring.core.constants.CommonConstants;
import com.tledu.spring.core.entity.AjaxResult;
import com.tledu.spring.core.tools.DemoAutoNewsCrawler;
import com.tledu.spring.core.vo.BasicVo;
import com.tledu.spring.domain.Cus;
import com.tledu.spring.domain.Role;
import com.tledu.spring.service.ICusService;
import com.tledu.spring.service.IRoleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 * @author Lsj
 * @version 1.0
 * @date 2021/7/5 9:10
 */
@Controller
@RequestMapping("/cus")
@AllArgsConstructor
public class CusController {
    private final ICusService cusService;
    private final IRoleService roleService;
    /**
     * 展示
     * @return 展示
     */
    @RequestMapping("listBefore")
    public String list(){
        return "cus/list";
    }

    /**
     * 搜索 展示
     * @param page
     * @param limit
     * @param name
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public BasicVo list(Integer page, Integer limit, String name){
        System.out.println("现在的name参数是"+name);
        if (name==null){
            return cusService.selectByAll(page,limit);
        }else {
            System.out.println("进入到需要name的方法中");
            return cusService.selectByAll(page,limit,name);
        }
    }
    /**
     * 添加数据
     * @return 添加
     */
    @RequestMapping("addBefore")
    public String add(Model model, HttpServletRequest request){
        // 这里面是个cus对象.里面有c_role表示权限
        System.out.println("request中的数据:"+request.getSession().getAttribute(CommonConstants.LOGIN_USER_SESSION_KEY));
        Cus cus = (Cus) request.getSession().getAttribute(CommonConstants.LOGIN_USER_SESSION_KEY);
        // 查询出role,之后是要看role中的权限
        Role role1 = roleService.selectAllByR_name(cus.getC_role());
        String r_x = role1.getR_auto();
        String[] tag = r_x.split(",");
        List<String> list = Arrays.asList(tag);
        if (list.contains("用户新增")){
            Role role = new Role();
            List<Role> lists = roleService.selectByAll(role);
            model.addAttribute("list",lists);
            return "cus/add";
        }
        return "/permissionDenied";
    }
    @RequestMapping("add")
    @ResponseBody
    public AjaxResult<String> add(Cus cus){
        cus.setC_password(DigestUtils.md5DigestAsHex(cus.getC_password().getBytes()));
        int m = cusService.insert(cus);
        if (m==1){
            return new AjaxResult<>("添加成功");
        }
        return new AjaxResult<>("添加失败");
    }
    /**
     * 修改
     * @param model
     * @param c_id
     * @return
     */
    @RequestMapping("updateBefore")
    public String update(Model model,Integer c_id,HttpServletRequest request){
        Cus cus = (Cus) request.getSession().getAttribute(CommonConstants.LOGIN_USER_SESSION_KEY);
        // 查询出role,之后是要看role中的权限
        Role role1 = roleService.selectAllByR_name(cus.getC_role());
        String r_x = role1.getR_auto();
        String[] tag = r_x.split(",");
        List<String> list = Arrays.asList(tag);
        if (list.contains("用户修改")){
            Cus cus1 = cusService.selectAllByC_id(c_id);
            model.addAttribute("cus",cus1);
            return "cus/update";
        }
        return "/permissionDenied";
    }
    @PostMapping("update")
    @ResponseBody
    public AjaxResult<String> update(Cus cus){
        int m = cusService.updateByC_id(cus,cus.getC_id());
        if (m==1){
            return new AjaxResult<>("添加成功");
        }
        return new AjaxResult<>("添加失败");
    }
    /**
     * 删除
     * @param c_id
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public AjaxResult<String > delete(String c_id,HttpServletRequest request){
        Cus cus = (Cus) request.getSession().getAttribute(CommonConstants.LOGIN_USER_SESSION_KEY);
        // 查询出role,之后是要看role中的权限
        Role role1 = roleService.selectAllByR_name(cus.getC_role());
        String r_x = role1.getR_auto();
        String[] tag = r_x.split(",");
        List<String> list = Arrays.asList(tag);
        if (list.contains("用户修改")){
            int m = cusService.deleteByC_id(c_id);
            if (m==1){
                return new AjaxResult<>("删除成功");
            }
            return new AjaxResult<>("删除失败");
        }
        return new AjaxResult<>("删除失败,可能是因为您没有该权限");
    }


    /**
     * 在页面输入网址,会在页面中显示其中的内容
     * @return
     */
    @RequestMapping("crawlerBlog")
    public String crawlerBlog(String bookName,Model model) throws Exception {
        DemoAutoNewsCrawler demoAutoNewsCrawler = new DemoAutoNewsCrawler("crawl",true,bookName);
        demoAutoNewsCrawler.start(1);
        model.addAttribute("otherHtml",demoAutoNewsCrawler.returnHtml());
        return "cus/crawlerBlog";
    }
    @RequestMapping("enterURL")
    public String enterURL(){
        return "cus/enterURL";
    }
}
